<?php

namespace App\PayType;

use App\Models\OrderGoodsModel;
use App\Models\sf_order;
use App\Models\ShopModel;
use App\Sevrice\OrderId;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Log;

class ZhiFuBao
{
    protected $config = [
        'app_id' => '2021000119691819',
        'notify_url' => 'http://www.news.com/api/payOrder',
        'return_url' => 'http://www.news.com/api/returnApi',
        'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlGmsRIG7cT+SSU4NXX+g/9EsZhrta7KClH1xMLZIlzIsQzbZxcRc5LFOvj2Hsl7T3wkB0UDDWaYhbXVoseoHjXt0DdvhJnHFePtTdSSbvfBDktbZS35omtxN/Wsmrux9Fk8iZZDlv623E3L0WrwE5aEz2nREAqLiHL9t61yPiRPSlZXZ+KTmEILaUcpAVqgYKCaPaI1RZhiqPeGZ06GLsjObEqcPFhnMNFjHJuOwYCc/gJnCbPHbGfY9WH77gqArRNc1RVee1BT9YCrGXC7PHk1fGHJgTJ9yh2TmTvCKC8UegFI+uMWSUMiHrZ1SqF0cPyf6IDLQayIu92T84jLvgQIDAQAB',
        // 加密方式： **RSA2**
        'private_key' => 'MIIEowIBAAKCAQEAh4m/NMjkEKkSeKGe/KEThBwzZVIA9+/1x+nmXwwHlIPj1M3yNeezfTQqGHhvd4GfHgmQOXKa5hz61DmTHAq/FGrg9MpT9sFf0UCC7fhA0hDtNFQdUhVs5pkcvpoWSrs24iNRG5+rAOrbWZruO3HGmz2QB6K3MWSIa6X4QXZLbt0Nkw+bRyt3xDAXRkU7YY3fXn1IO8a3YHmrgqPoRmjzW3NmjPcok4nfqp7cNykSLr+Bakz5C9zVpMAsS6fZPDPvkmIscIprrnqOo45CzOsJGo/Is8mRCqzQZv6kNEOLFTlp1FX7CNfWUC8spOnwtc015hB+gL1y/qRlq+tjicA3LwIDAQABAoIBAGoDfzBNVb4GULFUUWPupbI20KiWjLVh1s/ojs2XPjKXOtRPxf0N32G2NJrxUNab55Z6Cno0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DryEkd/4dF9XFCvWw3221JCkOgaFMm2AwB+ePrRIPluWg3fiUlu2/c+XH15IQvT/xkjk',
        // 使用公钥证书模式，请配置下面两个参数，同时修改ali_public_key为以.crt结尾的支付宝公钥证书路径，如（./cert/alipayCertPublicKey_RSA2.crt）
        // 'app_cert_public_key' => './cert/appCertPublicKey.crt', //应用公钥证书路径
        // 'alipay_root_cert' => './cert/alipayRootCert.crt', //支付宝根证书路径
        'log' => [ // optional
            'file' => './logs/alipay.log',
            'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
    ];

    public function index($order,$money)
    {
//        $order='202215895';
//        $money='120';
        $order = [
            'out_trade_no' => $order,
            'total_amount' => $money,
            'subject' => '支付宝支付',
        ];
        $alipay = Pay::alipay($this->config)->web($order);
        return $alipay;// laravel 框架中请直接 `return $alipay`
    }

    public function return()
    {
        $data = Pay::alipay($this->config)->verify(); // 是的，验签就这么简单！
        if($data->trade_no){
            sf_order::where('order_sn',$data->out_trade_no)->update(['order_status'=>1,'order_settlement_time'=>date('Y-m-d H:i:s'),'pay_channel'=>'支付宝']);
            return redirect('/api/payLast')->with('error','支付成功');
        }
        return redirect('/api/payLast')->with('error','支付失败');
        // 订单号：$data->out_trade_no
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
    }

    public function notify()
    {
        $alipay = Pay::alipay($this->config);

        try {
            $data = $alipay->verify(); // 是的，验签就这么简单！

            // 请自行对 trade_status 进行判断及其它逻辑进行判断，在支付宝的业务通知中，只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时，支付宝才会认定为买家付款成功。
            // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号；
            // 2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额）；
            // 3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）；
            // 4、验证app_id是否为该商户本身。
            // 5、其它业务逻辑情况
            Log::debug('Alipay notify', $data->all());
        } catch (\Exception $e) {
            // $e->getMessage();
        }
        return $alipay->success()->send();// laravel 框架中请直接 `return $alipay->success()`
    }
}